package org.example.boot.mybatis.pius.test;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.boot.mybatis.pius.bean.Stu;
import org.example.boot.mybatis.pius.bean.Student;
import org.example.boot.mybatis.pius.service.StuService;
import org.example.boot.mybatis.pius.service.StudentService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@SpringBootTest
public class App {
    @Resource
    private StuService service;
    @Resource
    private StudentService studentService;;

    @Test
    public void find() {
        List<Stu> list = service.list();
        list.forEach(stu -> {
            log.info("Stu:{}", stu);
        });
    }

    @Test
    public void save() {
        Stu stu = new Stu();
        stu.setName("张丰");
        stu.setSex("男");
        stu.setAge(18);
        stu.setTel("13991642660");
        stu.setScores("88");
        stu.setSchool("西安石油");
        stu.setZt(1);
        service.save(stu);
    }

    @Test
    public void findById() {
        Stu byId = service.getById(2);
        log.info("Stu:{}", byId);
    }

    @Test
    public void delete() {
        service.removeById(6);
    }

    @Test
    public void update() {
        Student student = new Student();
        student.setId(7);
        student.setAge(20);
        studentService.updateById(student);
    }

    @Test
    public void find1() {
        //按照学校进行查询
        //1产生wrapper对象
        QueryWrapper<Stu> wrapper = new QueryWrapper<>();
        //2设置条件
        wrapper.eq("school","商洛学院");
        //3查询
        List<Stu> list = service.list(wrapper);
        list.forEach(System.out::println);
    }

    @Test
    public void find2() {
        QueryWrapper<Stu> wrapper = new QueryWrapper<>();
        //=eq <>ne >gt >=ge <lt <=le
        wrapper.gt("age",17);
        List<Stu> list = service.list(wrapper);
        list.forEach(System.out::println);
    }

    @Test
    public void find3() {
        //模糊查询
        QueryWrapper<Stu> wrapper = new QueryWrapper<>();
//        wrapper.like("name","张");//%张%
//        wrapper.likeRight("name","张");//张%
//        wrapper.likeLeft("name","张");//%张
        wrapper.notLike("name","张");
        List<Stu> list = service.list(wrapper);
        list.forEach(System.out::println);
    }

    @Test
    public void find4() {
        QueryWrapper<Stu> wrapper = new QueryWrapper<>();
//        wrapper.orderByAsc("id");
        wrapper.orderByDesc("id");
        List<Stu> list = service.list(wrapper);
        list.forEach(System.out::println);
    }

    @Test
    public void find5() {
        QueryWrapper<Stu> wrapper = new QueryWrapper<>();
        wrapper.select("school","age");
        List<Stu> list = service.list(wrapper);
        list.forEach(System.out::println);
    }

    @Test
    public void find6() {
        QueryWrapper<Stu> wrapper = new QueryWrapper<>();
        wrapper.groupBy("school");
        wrapper.select("school","count(0)");
        service.list(wrapper);
        List<Stu> list = service.list(wrapper);
        list.forEach(System.out::println);

    }

    @Test
    public void find7() {
        QueryWrapper<Stu> wrapper = new QueryWrapper<>();
        wrapper.last("and school='商洛学院'");
        service.list(wrapper);
        List<Stu> list = service.list(wrapper);
        list.forEach(System.out::println);
    }

    @Test
    public void find8() {
        QueryWrapper<Stu> wrapper = new QueryWrapper<>();
        //如果写入多个条件是and
        wrapper.eq("age","18");
        wrapper.eq("school","西安石油");
        service.list(wrapper);
        List<Stu> list = service.list(wrapper);
        list.forEach(System.out::println);
    }

    @Test
    public void find9() {
        QueryWrapper<Stu> wrapper = new QueryWrapper<>();
        //如果写入多个 使用or
        wrapper.eq("age","18")
                        .or().eq("school","西安邮电");
        service.list(wrapper);
        List<Stu> list = service.list(wrapper);
        list.forEach(System.out::println);
    }

    @Test
    public void find10() {
        QueryWrapper<Stu> wrapper = new QueryWrapper<>();
        //如果写入多个条件，复杂情况
        wrapper.eq("age","18");
        wrapper.eq("scores",88)
                .or().eq("school","商洛学院");
        service.list(wrapper);
        List<Stu> list = service.list(wrapper);
        list.forEach(System.out::println);
    }

    @Test
    public void
    update1(){
        UpdateWrapper<Stu> wrapper = new UpdateWrapper<>();
        wrapper.eq("name","张三丰"); // 改谁
        wrapper.set("name","二明");
        service.update(wrapper);
        List<Stu> list = service.list(wrapper);
        list.forEach(System.out::println);
    }

    @Test
    public void update2() {
        List<Stu> list = new ArrayList<>();
        Stu e1 = new Stu();
        e1.setAge(17);
        e1.setId(3);
        list.add(e1);
        Stu e2 = new Stu();
        e2.setAge(15);
        e2.setId(4);
        list.add(e2);
        Stu e3 = new Stu();
        e3.setAge(9);
        e3.setId(9);
        list.add(e3);
        service.piliangcaozuo(list);
    }
}
